from PyQt4 import QtCore, QtGui
from ui_publishdialog import Ui_Dialog
import sys
import batch_tile

class Dialog(QtGui.QDialog):
    def __init__(self, image_dir):
        QtGui.QDialog.__init__(self)

        self.ui = Ui_Dialog()
        self.ui.setupUi(self)

        self.ui.imagesLineEdit.setText(image_dir)

        self.connect(self.ui.imagesdirButton, QtCore.SIGNAL("clicked()"),
                     self.select_images_dir)
        self.connect(self.ui.mrsidButton, QtCore.SIGNAL("clicked()"),
                     self.select_mrsid_exe)
        self.connect(self.ui.gdal2tilesButton, QtCore.SIGNAL("clicked()"),
                     self.select_gdal2tiles_exe)
        self.connect(self.ui.kmldirButton, QtCore.SIGNAL("clicked()"),
                     self.select_kml_dir)
        self.connect(self.ui.fgdcdirButton, QtCore.SIGNAL("clicked()"),
                     self.select_fgdc_dir)

        self.connect(self.ui.publishPushButton, QtCore.SIGNAL("clicked()"),
                     self.process)

        self.connect(self.ui.cancelPushButton, QtCore.SIGNAL("clicked()"),
                     self.reject)

        self.connect(self.ui.typeComboBox, QtCore.SIGNAL("currentIndexChanged(const QString &)"),
                     self.type_changed)

    def type_changed(self, text):
        if text == 'MrSID':
            self.ui.mrsidLabel.setEnabled(True)
            self.ui.mrsidLineEdit.setEnabled(True)
            self.ui.mrsidButton.setEnabled(True)
        else:
            self.ui.mrsidLabel.setEnabled(False)
            self.ui.mrsidLineEdit.setEnabled(False)
            self.ui.mrsidButton.setEnabled(False)

    def sizeHint(self):
        return QtCore.QSize(552, 383)

    def select_images_dir(self):
        dir = QtGui.QFileDialog.getExistingDirectory()
        if dir != '':
            self.ui.imagesLineEdit.setText(dir)

    def select_mrsid_exe(self):
        dir = QtGui.QFileDialog.getOpenFileName()
        if dir != '':
            self.ui.mrsidLineEdit.setText(dir)

    def select_gdal2tiles_exe(self):
        dir = QtGui.QFileDialog.getOpenFileName()
        if dir != '':
            self.ui.gdal2tilesLineEdit.setText(dir)

    def select_kml_dir(self):
        dir = QtGui.QFileDialog.getExistingDirectory()
        if dir != '':
            self.ui.kmlLineEdit.setText(dir)

    def select_fgdc_dir(self):
        dir = QtGui.QFileDialog.getExistingDirectory()
        if dir != '':
            self.ui.fgdcLineEdit.setText(dir)

    def process(self):
        prefix = str(self.ui.kmlprefixLineEdit.text());
        if not prefix.endswith('/'):
            prefix = prefix + '/'

        progress = QtGui.QProgressDialog("Publishing images...", "Cancel", 0, 0, None)
        progress.setWindowModality(QtCore.Qt.WindowModal);
        
        if self.ui.typeComboBox.currentText() == 'MrSID':
            image_type = '.sid'
        elif self.ui.typeComboBox.currentText() == 'TIFF':
            image_type = '.tif'
            
        batch_tile.process(str(self.ui.imagesLineEdit.text()),
                           prefix,
                           str(self.ui.kmlLineEdit.text()),
                           str(self.ui.fgdcLineEdit.text()),
                           str(self.ui.mrsidLineEdit.text()),
                           str(self.ui.gdal2tilesLineEdit.text()),
                           str(self.ui.hostnameLineEdit.text()),
                           str(self.ui.dbLineEdit.text()),
                           str(self.ui.usernameLineEdit.text()),
                           str(self.ui.passwordLineEdit.text()),
                           progress,
                           self.ui.recursiveCheckBox.isChecked(),
                           image_type)
        self.accept()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Dialog()
    window.show()
    sys.exit(app.exec_())
    
